package ink.ykb.mq.subscrib;

import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.rabbitmq.client.Channel;

import ink.ykb.mq.config.DelayTime;
import ink.ykb.mq.config.RabbitTemplateUtil;
import ink.ykb.mq.config.WorkQueueRabbitConfig;
import lombok.extern.slf4j.Slf4j;

/**
 * 订阅者
 * @author mading
 *
 */
@Component
@Slf4j
public class Receive {
  @Autowired
  private RabbitTemplateUtil rabbitTemplateUtil;
  
  @RabbitListener(queues=WorkQueueRabbitConfig.DEFULT_QUEUE)
  public void defultProcess(String msg,Channel channel) throws JsonProcessingException {
    log.info("defultProcess接收消息：{}",msg);
  }
  
  @RabbitListener(queues=WorkQueueRabbitConfig.TEST_QUEUE)
  public void testProcess(String msg,Channel channel) throws JsonProcessingException {
      log.info("testProcess接收消息：{}",msg);
  }
  
  @RabbitListener(queues=WorkQueueRabbitConfig.TEST_ONE_QUEUE)
  public void testOneProcess(String msg) {
    log.info("testOneProcess接收消息：{}",msg);
  }

  @RabbitListener(queues= WorkQueueRabbitConfig.WORK_TEST_QUEUE)
  public void workQueueProcess(Message message) {
	String msg = new String(message.getBody());
    log.info("workQueueProcess接收消息：{}",msg);
    MessageProperties messageProperties = message.getMessageProperties();
    
    Map<String, Object> headers = messageProperties.getHeaders();
    String uuid = (String)headers.get("uuid");
    if(uuid == null) {
    	uuid = UUID.randomUUID().toString();
    }
    Integer count = (Integer)headers.get("count");
    if(count == null || count <= 0) {
    	count = 1;
    }else {
    	count = count.intValue() +1;
    }
    if(count >= 18) {
      count = 18;
    }
    
    
    headers.put("count",count);
    headers.put("uuid",uuid);
    
    //=====================插件方式实现==============================
//    Integer delay = 2000;
//    delay = (int) (delay * (Math.pow(2,(count.intValue() -1))));
//  messageProperties.setDelay(delay);
    
    //=====================TTL和DLX实现==============================
    DelayTime delayTime = DelayTime.values()[count];
	Integer delay = delayTime.getTime();
	
	
	log.info("=================================================>");
    log.info("此任务{}第{}次处理",headers.get("uuid"),headers.get("count"));
    
    try {
      log.info("任务处理中........");
      
      //随机处理错误
      Random r = new Random();
      int num = r.nextInt(3);
      if(num != 0) {
        throw new RuntimeException("任务处理失败");
      }
      log.info("任务处理成功！！！！");
    } catch (Exception e) {
      log.error("workQueueProcess处理任务{}失败，下次处理延迟{}毫秒，异常：{}",headers.get("uuid"),delay,e.getMessage());
      
      //=====================TTL和DLX实现==============================
      messageProperties.setHeader("delay-exchange", WorkQueueRabbitConfig.WORK_TEST_EXCHANGE);
      messageProperties.setHeader("delay-routing-key", WorkQueueRabbitConfig.WORK_TEST_QUEUE);
      rabbitTemplateUtil.sendDelayMsg(delayTime,message);
      log.info("workQueueProcess发送消息：{}",msg);
    }

    
    
  }

}
